import {defineStore} from 'pinia'
// 导入 worth 相关api
import { getWorthSwiperData,getWaterFlowData } from '@/api/worth'
// 导入 worth 相关类型
import type { Result$0Type ,waterFlow} from '@/api/modules/worthType'

const useWorthStore = defineStore('worthStore',{
    state:() => {
        return{
            swiperData:{} as Result$0Type,
            waterFlowData_:{} as waterFlow,
            page:1
        }
    },
    actions:{
        // 请求swiper数据
        async getSwiperData(){
            try {
                this.swiperData = await getWorthSwiperData()
                
            } catch (e) {
                return Promise.reject('获取失败')
            }
        },
        // 请求waterFlow数据
        async getWaterData(){
            try {
                this.waterFlowData_ = await getWaterFlowData() 
            // const res =  await getWaterFlowData() 
            // console.log(res,`res`);
            } catch (e) {
                return Promise.reject('获取失败')
            }
        },
        // 获取更多数据库
        async getMoreData(){
            // console.log(this.page);
            try {
                this.page +=1
                const res= await getWaterFlowData(this.page,2) 
                // console.log(res.result);
                this.waterFlowDataList[0].topics.unshift(...res.result[0].topics,...res.result[1].topics) 
                // console.log(this.waterFlowDataList[0].topics.unshift(...res.result[0].topics,...res.result[0].topics),123);
                
            } catch (e) {
                return Promise.reject('获取失败')
            }

        }
    },
    getters : {
        // 处理请求swiper的数据
        SwiperList:(state) => {
            // console.log(state.swiperData);
            return (state.swiperData || {}).navList || []
        },
        waterFlowDataList:(state)=>{
            // console.log(state.waterFlowData_.result,`state`);
            return (state.waterFlowData_ || {} ).result || []
            // console.log(state.waterFlowData_,`state.waterFlowData_`);
            
        }
    }
})

export default useWorthStore